package com.shuqi.common;

import java.io.File;
import java.io.FileFilter;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;

import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.SharedPreferenceUtil;
import com.shuqi.common.utils.Util;
import com.shuqi.controller.R;

public class LoadingImg {


    /**
     * 获取需要显示的启动图或开屏自主广告图
     * 
     * @param context
     * @param key SharedPreferences的key
     * @param filePath 文件路径
     * @return
     */
    public static Drawable getLoadingPic(Context context, String key, String filePath) {
        Drawable drawable = null;
        String name =
                context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_CONFIG,
                        Context.MODE_PRIVATE).getString(key, "");
        if (!TextUtils.isEmpty(name) && Util.isCanUseSdCard()) {// 有新图
            File file = new File(filePath, name);
            if (file.exists()) {
                drawable = Drawable.createFromPath(file.getAbsolutePath());
            }
        }
        if (drawable == null) {
            drawable = context.getResources().getDrawable(R.drawable.img_loading);
        }
        return drawable;
    }

    public static Drawable getLoadingAdPic(Context context, String fileName) {
        Drawable drawable = null;
        String name =
                context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_CONFIG,
                        Context.MODE_PRIVATE).getString(fileName, "");
        if (!TextUtils.isEmpty(name) && Util.isCanUseSdCard()) {// 有新图
            File file = new File(Config.DEFAULT_PATH_LOADINADGPIC, name);
            if (file.exists()) {
                drawable = Drawable.createFromPath(file.getAbsolutePath());
            }
        }
        return drawable;
    }

    /**
     * 缓存新图
     * 
     * @param context
     * @param url
     * @param key SharedPreferences的key
     * @param filePath 文件路径
     */
    public static void downloadLoadingPic(Context context, String url, String key, String filePath) {
        Log4an.w("yhw.LoadingImg", "1.downloadLoadingPic...");
        if (TextUtils.isEmpty(url) || !Util.isCanUseSdCard()) {
            Log4an.w("yhw.LoadingImg", "1.url is null or SDcard can't use");
            return;
        }
        Log4an.w("yhw.LoadingImg", "2.downloadLoadingPic...开始下载");
        // 检查是否下载过
        final String name = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
        File file = new File(filePath + name);
        Log4an.w("yhw.LoadingImg", "context is null=" + (context == null));
        SharedPreferences sp =
                context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_CONFIG,
                        Context.MODE_PRIVATE);
        if (file.exists()) {
            if (!sp.getString(key, "").equals(name)) {
                sp.edit().putString(key, name).commit();
            }
            Log4an.w("yhw.LoadingImg", "3.文件已存在：" + name);
            return;
        }
        Log4an.w("yhw.LoadingImg", "4.开始下载");
        // 下载
        Drawable drawable =
                AsyncImageLoader.loadImageFromUrl(url, AsyncImageLoader.ImageType_LoadingPic,
                        context.getApplicationContext(), filePath);
        Log4an.w("yhw.LoadingImg", "5.下载完成");
        if (drawable != null) {
            sp.edit().putString(key, name).commit();
            try {
                File folder = new File(filePath);
                if (folder.exists()) {
                    File[] fs = folder.listFiles(new FileFilter() {
                        @Override
                        public boolean accept(File pathname) {
                            return pathname.isFile();
                        }
                    });
                    for (File f : fs) {
                        if (!f.getName().endsWith(name)) {
                            f.delete();
                        }
                    }
                }
            } catch (Exception e) {
                Log4an.w("yhw.LoadingImg", e.toString());
            }
        }
    }
}
